package org.datastealer.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：用于测试连接数据库的简单查询控制层
 * @date ：Created in 2020/9/10
 */
@RestController
@RequestMapping(path = "/demo")
public class DemoController {

    private static final String HELLO_SQL = "SELECT 'hello' FROM DUAL";

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping("/hello")
    public String hello() {
        return jdbcTemplate.queryForObject(HELLO_SQL, String.class);
    }

    /**
     * 根据sql查询字符串
     *
     * @param sql
     * @return
     */
    @GetMapping("/queryForString")
    public String queryForString(@RequestParam("sql") String sql) {
        return jdbcTemplate.queryForObject(sql, String.class);
    }

    /**
     * 根据sql查询Map对象
     *
     * @param sql
     * @return
     */
    @GetMapping("/queryForMap")
    public Object queryForMap(@RequestParam("sql") String sql) {
        return jdbcTemplate.queryForMap(sql);
    }

    /**
     * 根据sql查询List
     *
     * @param sql
     * @return
     */
    @GetMapping("/queryForList")
    public Object queryForList(@RequestParam("sql") String sql) {
        return jdbcTemplate.queryForList(sql);
    }

}